<?php


namespace app\admin\controller;


use think\Controller;
use gmars\rbac\Rbac;
use think\Db;

class Position extends Controller
{
    /**
     * 加载角色配置页面
     * @return \think\response\View
     */
    public function index()
    {
        //所有权限
        $list = Db::name('permission')
            ->field(['name', 'id'])
            ->select();
//        dump($list);


        $data1 = Db::name('role')
            ->field('name,id,description,status')
            ->select();
//        return view('position/index',[
//            'data'=>$data
//        ]);
        $permission = Db::name('permission')
            ->field('name,id')
            ->select();


        $count = count($data1);
        $data = Db::name('role')->paginate(2, $count);
// 把分页数据赋值给模板变量list
        $this->assign('da', $data);
        $this->assign('permission', $permission);
        $this->assign('list', $list);
        return view('position/index');
    }

    /**
     * 加载权限管理页面
     * @return \think\response\View
     */
    public function authority()
    {
        $data1 = Db::name('permission')
            ->field('name,id,description,status,path')
            ->select();


        $count = count($data1);
        $data = Db::name('permission')->paginate(2, $count);
        $this->assign('da', $data);
        return view('position/authority');
    }

    /**
     * 添加角色
     * @return mixed
     * @throws \think\Exception
     */
    public function addrole($permission)
    {
        $rbac = new Rbac();
        $description = input('post.description');
        $role = input('post.role');

         $permission =  implode(',',$permission);
            $res = $rbac->createRole([
                'name' => $role,
                'description' => $description,
                'status' => 1
            ], $permission);

            return $res['status'];

        }


        /**
         * 删除角色
         * @param $id
         * @return int
         * @throws \think\Exception
         * @throws \think\exception\PDOException
         */
        public
        function delrole($id)
        {
            $res = Db::name('role')
                ->delete($id);

            $res2 = Db::name('role_permission')
                ->where('role_id', $id)
                ->delete();

            if ($res && $res2 && 1) {
                return $res;
            }
            return $res;
        }


        /**
         * 添加权限
         * @return mixed
         * @throws \think\Exception
         */
        public
        function addPermission()
        {
            $description = input('post.description');
            $name = input('post.name');
            $path = input('post.path');
            $rbac = new Rbac();
            $res = $rbac->createPermission([
                'name' => $name,
                'description' => $description,
                'status' => 1,
                'type' => 1,
                'category_id' => 1,
                'path' => $path,
            ]);
            return $res['status'];
        }

        /**
         * 删除权限
         * @param $id
         * @return int
         * @throws \think\Exception
         * @throws \think\exception\PDOException
         */
        public
        function delpermission($id)
        {
            $res = Db::name('permission')
                ->delete($id);
            return $res;
        }

        /**
         * 该角色对应的所有权限
         * @param $id
         * @return false|\PDOStatement|string|\think\Collection
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         */
        public
        function permissionall($id)
        {

            $list = Db::name('role')->alias('r')
                ->join('role_permission rp', 'r.id=rp.role_id')
                ->join('permission p', 'p.id=rp.permission_id')
                ->field(['r.id', 'p.name'])
                ->where('r.id', $id)
                ->select();
//        dump($list);
            return $list;
        }


    }